package com.yuhongwei.javaproject.service;

import com.alibaba.fastjson.JSONObject;
import com.yuhongwei.javaproject.Constants;
import com.yuhongwei.javaproject.net.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: jiangjiwei
 * @email: jiangjiwei@3452788854qq.com
 * @desc: 该类用于实现通过rpc调用比特币客户端的getbestblockhash功能
 * @datetime: 2021/12/28 11:09
 */
public class GetBestBlockHashService {

    public  String getBesttBlockHash(){

        // 装备rpc请求的用户的身份信息
        String auth = Constants.BITCOIN_RPC_USER+":"+Constants.BITCOIN_RPC_PWO;
        // java中实现base64编码
        Base64.Encoder encoder = Base64.getEncoder();
        String authBase64 = encoder.encodeToString(auth.getBytes());


        // 装备请求头信息
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic " +authBase64);

        // 准备要发送的rpc数据
        Map<String,Object> boby = new HashMap<>();
        boby.put("id", 1000);
        boby.put("method","getbestblockhash");
        boby.put("params",null);
        boby.put("json_rpc","2.0");
        // 将map转换成byte[]
        byte[] bodyBaytes = JSONObject.toJSONBytes(boby);
        // 格式：{"id": 1001, "result":xxxx, "error":null}
        String result = HttpUtils.post(Constants.BITCOIN_RPC_URL,header,bodyBaytes);
        return null;

    }
}
